package com.IOT_LSL;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;

public class Dialog_Set_Btn extends Dialog implements View.OnClickListener {
    private EditText dialog_btn_name;
    private EditText dialog_down_data;
    private EditText dialog_up_data;
    private Button dialog_confirm;
    private Button dialog_cancel;

    private Activity context1;
    private SharedPreferences sp;
    private String old_down_data;
    private String old_up_data;
    private String old_btn_name;
    private String btn_no;
    private int no;

    public Dialog_Set_Btn(@NonNull Context context, int themeResId) {
        super(context);
        this.context1 = (Activity) context;
        this.no = themeResId;
        btn_no = "btn"+themeResId;
    }

    public Dialog_Set_Btn(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_set_btn);
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        Point size = new Point();
        d.getSize(size);
        p.width = (int)(size.x * 0.8);
        getWindow().setAttributes(p);
        sp = getContext().getSharedPreferences("btn_set_data",Context.MODE_PRIVATE);
        old_down_data = sp.getString(btn_no+"_down_data",null);
        old_up_data = sp.getString(btn_no+"_up_data",null);
        old_btn_name = sp.getString(btn_no+"_name",null);
        Init_Widget();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dialog_confirm:
                SharedPreferences.Editor editor = sp.edit();
                editor.putString(btn_no+"_down_data",dialog_down_data.getText().toString());
                editor.putString(btn_no+"_up_data",dialog_up_data.getText().toString());
                editor.putString(btn_no+"_name",dialog_btn_name.getText().toString());
                editor.commit();
                Button bt_temp = (Button) context1.findViewById(no);
                bt_temp.setText(dialog_btn_name.getText().toString());
                dismiss();
                break;
            case R.id.dialog_cancel:
                dismiss();
                break;
        }
    }

    private void Init_Widget() {
        dialog_btn_name = findViewById(R.id.dialog_btn_name);
        dialog_down_data = findViewById(R.id.dialog_down_data);
        dialog_up_data = findViewById(R.id.dialog_up_data);
        dialog_confirm = findViewById(R.id.dialog_confirm);
        dialog_cancel = findViewById(R.id.dialog_cancel);
        dialog_btn_name.setCursorVisible(false);
        dialog_down_data.setCursorVisible(false);
        dialog_up_data.setCursorVisible(false);
        dialog_confirm.setOnClickListener(this);
        dialog_cancel.setOnClickListener(this);
        dialog_btn_name.setText(old_btn_name);
        dialog_up_data.setText(old_up_data);
        dialog_down_data.setText(old_down_data);
    }
}
